layui.define(['jquery', 'layer', 'rate', 'upload', 'form', 'laytpl', 'http', 'laydate', 'rntable', 'table', 'public', 'iconHhysFa', 'slider', 'tinymce', 'iconPicker', 'tagsinput'], function (exports) {
    "use strict";

    var MOD_NAME = 'rnform',
        $ = layui.jquery,
        layer = layui.layer;
    var FormConfig = [];
    var dateall = {};
    var datedata = {};
    var context = new function () {
        this.GetFormData = function (arr = '', elem = '') {

            var data = {};
            var list = FormConfig;
            if (arr) {
                var list = arr;
            }
            for (var i = 0; i < list.length; i++) {
                var children = list[i].children || [];
                for (var ii = 0; ii < children.length; ii++) {
                    var item = children[ii]['children'] || [];
                    for (var iii = 0; iii < item.length; iii++) {
                        list.push(item[iii])
                    }
                }
            }
            for (var i = 0; i < list.length; i++) {
                var field = list[i].field || '';
                var tag = list[i].tag || '';
                var elem_id=list[i].elem_id || '';
                if (field) {
                    data[field] = $("" + elem + "input[name='" + field + "']," + elem + "textarea[name='" + field + "']," + elem + "select[name='" + field + "']").val();
                    switch (tag) {
                        case "datetime":
                            var range_field = list[i].range_field || '';
                            if (range_field) {
                                data[range_field] = $("input[name='" + range_field + "']").val();
                            }
                            break;
                        case "showtree":
                            var checkedData = layui.tree.getChecked(elem_id);
                            var StrData=layui.rnform.gettreedata(checkedData);
                            data[field]=StrData.replace(/,([^,]*)$/, "$1")
                            break;
                        case "switch":
                            data[field] = $("" + elem + "input[name='" + field + "']").is(":checked") ? '1' : '2';
                            break;
                        case "checkbox":
                            var checkbox = '';
                            $("" + elem + "input[name='" + field + "']:checked").each(function () {
                                checkbox += $(this).attr('value') + ',';
                            })
                            data[field] = (checkbox.substring(checkbox.length - 1) == ',') ? checkbox.substring(0, checkbox.length - 1) : checkbox;
                            break;
                        case "radio":
                            data[field] = $("" + elem + "input[name='" + field + "']:checked").val() || '';
                            break;
                        case "jsontable":
                            var json = $("" + elem + "textarea[name='" + field + "']").val() || '[]';
                            data[field] = eval(json);
                            break;
                    }
                }
            }
            return data;
        }
        this.GetFollow = function (obj) {
            var value = $(obj).val();
            layui.layer.prompt({title: '请输入信息', formType: 2}, function (val, index) {
                if (val) {
                    if (value) {
                        var content = layui.public.DateNow() + '  ' + val + '\r\n' + value;
                    } else {
                        var content = layui.public.DateNow() + '  ' + val;
                    }
                    $(obj).val(content);
                }
                layui.layer.close(index);
            });
        }

        this.genId = function () {
            return (Math.random() * 10000000).toString(16).substr(0, 4) + '_' + (new Date()).getTime() + '_' + Math.random().toString()
                .substr(2, 5);
        }


        this.GetForm = function (arr = [], elem = '') {

            var str = '';
            for (var i = 0; i < arr.length; i++) {
                var data = arr[i];
                var tag = data.tag;
                arr[i]['elem_id'] = data['elem_id'] || layui.rnform.genId();
                arr[i]['value'] = data['value'] || '';
                var range = data['range'] || '';
                var placeholder = data['placeholder'] || '';
                if (range && placeholder == '') {
                    arr[i]['placeholder'] = '开始日期 ' + range + ' 结束日期';
                }
                var list = data.data || [];
                var options_num = data.options_num || '';
                if (list.length == 0 && options_num) {
                    layui.http.SendAjax('/ruinuo/design.form/getoptions', {num: options_num}, [], function (res) {
                        arr[i]['data'] = res.data || [];
                    })
                }
                if (tag=='tags')
                {
                    arr[i]['placeholder'] = '回车确定';
                }



                switch (tag) {
                    case "input":
                    case "number":
                    case "phone":
                    case "email":
                    case "password":
                    case "tags":
                    case "openselect":
                    case "icon":
                    case "datetime":
                    case "upimg":
                    case "upfile":
                    case "jsonshow":
                    case "showtree":
                        str += layui.rnform.LineText(data);
                        break;
                    case "textarea":
                    case "htmledit":
                        str += layui.rnform.RowsText(data);
                        break;
                    case "switch":
                        str += layui.rnform.switch(data);
                        break;
                    case "radio":
                        str += layui.rnform.radio(data);
                        break;
                    case "checkbox":
                        str += layui.rnform.checkbox(data);
                        break;
                    case "select":
                    case "cascader":
                    case "tree":
                    case "dateall":
                        str += layui.rnform.select(data);
                        break;
                    case "color":
                        str += layui.rnform.color(data);
                        break;
                    case "rate":
                        str += layui.rnform.rate(data);
                        break;
                    case "slider":
                        str += layui.rnform.slider(data);
                        break;
                    case "tips":
                    case "legend":
                        str += layui.rnform.TipsLegend(data);
                        break;
                    case "grid":
                        str += layui.rnform.grid(data);
                        break;
                    case "card":
                        str += layui.rnform.card(data);
                        break;
                    case "tab":
                        str += layui.rnform.tab(data);
                        break;
                    case "flex":
                        str += layui.rnform.flex(data);
                        break;
                    case "jsontable":
                        str += layui.rnform.JsonTable(data);
                        break;
                }
            }
            if (elem != '') {
                FormConfig = arr;
            }
            if (elem == '') {
                return str;
            }
            $(elem).html(str);
            layui.rnform.BindAction();
            layui.form.render();

            try {
                CheckboxAction();
                RadioAction();
                SelectAction();
                SwitchAction();
            } catch (err) {
            }
        }

        /*绑定事件*/
        this.BindAction = function (data = []) {
            layui.element.init();
            if (data.length != 0) {
                var arr = data;
            } else {
                var arr = FormConfig;
            }

            for (var i = 0; i < arr.length; i++) {
                var data = arr[i];
                var tag = data.tag;
                var elem_id = data.elem_id || '';
                var elem = "#focus_" + elem_id;
                var type = data.date_cat || 'date';
                var field = data.field || '';
                var range = data.range || false;
                var range_field = data.range_field || '';
                var value = data.value || '';
                /*循环绑定事件*/
                var children = data.children || [];
                for (var ii = 0; ii < children.length; ii++) {
                    var item = children[ii]['children'] || [];
                    if (item.length != 0) {
                        layui.rnform.BindAction(item);
                    }
                }


                switch (tag) {
                    case "showtree":
                        var rowsdata=data.data.rows || [];
                        var treedata=layui.rnform.treedata(rowsdata,value);
                        layui.tree.render({
                            elem: "#showtree_" + elem_id,
                            showCheckbox: true,
                            data: treedata,
                            id:elem_id,
                            customName: {
                                id: 'value',
                                title: 'label',
                                children: 'children'
                            }
                        });
                        break;
                    case "datetime":
                        datedata['focus_' + elem_id] = data;
                        layui.laydate.render({
                            elem: elem,
                            type: type,
                            fullPanel: true,
                            range: range,
                            done: function (value, date, endDate) {
                                var id = $(this.elem).attr('id');
                                var Idata = datedata[id];
                                var a_field = Idata.field || '';
                                var b_field = Idata.range_field || '';
                                var c_range = Idata.range || '';
                                if (b_field) {
                                    var itme = value.split(c_range);
                                    var v1 = itme[0] || '';
                                    var v2 = itme[1] || '';
                                    $("input[name='" + a_field + "']").val(v1.trim());
                                    $("input[name='" + b_field + "']").val(v2.trim());
                                }
                            }
                        });
                        break;
                    case "dateall":
                        var ids = 'laydate_' + elem_id;

                        var initValue = '' + data.value + '' || '';
                        if (initValue) {
                            initValue = initValue.split(',');
                        } else {
                            initValue = [];
                        }
                        dateall[ids] = xmSelect.render({
                            el: elem,
                            content: '<div id="' + ids + '" />',
                            height: 'auto',
                            autoRow: true,
                            name: field,
                            on: function (data) {
                                if (!data.isAdd) {
                                    layui.rnform.dateSelect(dateall[ids].getValue('value'), ids);
                                }
                            }
                        })

                        layui.laydate.render({
                            elem: '#' + ids,
                            position: 'static',
                            showBottom: false,
                            type: type,
                            change: function () {
                                layui.rnform.dateSelect(dateall[ids].getValue('value'), ids);
                            },
                            done: function (value) {
                                var values = dateall[ids].getValue('value');
                                var index = values.findIndex(function (val) {
                                    return val === value
                                });
                                if (index != -1) {
                                    values.splice(index, 1);
                                } else {
                                    values.push(value);
                                }
                                layui.rnform.dateSelect(values, ids);
                                dateall[ids].update({
                                    data: values.map(function (val) {
                                        return {
                                            name: val,
                                            value: val,
                                            selected: true,
                                        }
                                    })
                                })
                            },
                            ready: function(date){
                                dateall[ids].update({
                                    data: initValue.map(function (val) {
                                        return {
                                            name: val,
                                            value: val,
                                            selected: true,
                                        }
                                    })
                                })
                                layui.rnform.dateSelect(dateall[ids].getValue('value'), ids);
                                //layui.rnform.removeAll(ids);
                            },

                        })


                        break;
                    case "range_time":
                        var field = data.field || '';
                        var fields = field.split(',');
                        var start = '#' + fields[0];
                        var end = '#' + fields[1];
                        layui.laydate.render({
                            elem: elem,
                            type: type,
                            range: [start, end],

                        });
                        break;

                    case "color":
                        c
                        break;
                    case "slider":
                        var min = data['min_val'] || 1;
                        var max = data['max_val'] || 100;
                        layui.slider.render({
                            elem: '#slider_' + elem_id,
                            input: true, //输入框
                            theme: theme_color,
                            value: data.value || 0,
                            min: min,
                            max: max,
                            done: function (value) {
                                console.log(123);
                                var obj = $(this)[0].elem;
                                var field = $(obj).attr('data-id');
                                $("#" + field).val(value);
                                SelectAction();
                            }
                        });
                        break;
                    case "rate":
                        layui.rate.render({
                            elem: '.focus_' + elem_id,
                            theme: theme_color,
                            value: data.value || 0,
                            choose: function (value) {
                                var obj = $(this)[0].elem;
                                var field = $(obj).attr('data-id');
                                $("#" + field).val(value);
                            }
                        });
                        break;
                    case "htmledit":
                        layui.tinymce.render({
                            elem: elem,
                            height: data.height,
                            forced_root_block: '',
                            valid_elements: '*[*]',
                            valid_children: '*[*]',
                            images_upload_url: '/admin/file/upload/?t=tinymce',
                            setup: function (editor) {
                                editor.on('change', function () {
                                    editor.save();
                                });
                            }
                        });
                        break;
                    case "icon":
                        var value = $(elem).val() || 'icon-keyboard-9';
                        layui.iconHhysFa.render({
                            elem: elem,
                            type: 'awesome',
                            url: '/static/component/pear/module/icon/icon.css',
                            search: true,
                            page: true,
                            limit: 16,
                            value: value,
                            click: function (data) {
                                SelectAction();
                            },
                            success: function (d) {

                            }
                        });
                        break;
                    case "select":
                    case "cascader":
                    case "tree":
                        layui.rnform.SelectAction(arr[i])
                        break;
                    case "upimg":
                    case "upfile":
                        layui.rnform.UploadAction(arr[i])
                        break;
                    case "tags":
                        $(elem).tagsInput({
                            width: 'auto',
                            defaultText: data.placeholder,
                            height: '26px',
                        })
                        break;
                    case "jsontable":
                        var elem_id=arr[i].elem_id;
                        var tableid = 'table_' + elem_id;
                        var value = arr[i].value || [];
                        var cols = arr[i].cols || [];
                        var data_cat = arr[i].data_cat || '';
                        datedata[elem_id] = arr[i];
                        var text='新增';
                        var title='<div class="pear-btn pear-btn-danger pear-btn-smm" onclick="layui.rntable.AddTable(\'' + tableid + '\')">'+text+'</div>';
                        if (data_cat)
                        {
                            if (data_cat == 'choose') {
                                text='选择';
                            }
                            title='<div class="pear-btn pear-btn-danger pear-btn-smm" onclick="layui.rnform.OpenChoose(\'' + elem_id + '\')">'+text+'</div>';
                        }
                       var curd=arr[i].curd || 1;
                       if (curd==1)
                       {
                           var caozuo = {
                               title: title,
                               width: 60,
                               tpl: '<div><button type="button" class="pear-btn pear-btn-danger pear-btn-smm" onclick="layui.rntable.DeleteTable(\'' + tableid + '\',{{d.LAY_INDEX}})"><i class="layui-icon layui-icon-delete"></i></button><button type="button" class="pear-btn pear-btn-primary pear-btn-smm ruinuo-drag"><i class="layui-icon layui-icon-slider"></i></button></div>'
                           };
                           cols.push(caozuo);
                       }

                        var table = {
                            style: 1,
                            elem: tableid,
                            limit: 0,
                            page: false,
                            cols: layui.rntable.GetCols(cols, tableid),
                        }
                        layui.rntable.PtTable(table, value);
                        break;
                }

            }

        }

        this.treedata=function (arr,value)
        {
            var values=value.split(',');
            for (var i = 0; i < arr.length; i++) {

                var val=arr[i]['value'] || '';
                var children=arr[i].children || [];



                if (children.length!=0)
                {
                    arr[i].children=layui.rnform.treedata(children,value);
                    arr[i].spread=true;
                }
                else
                {
                    for (var ii = 0; ii < values.length; ii++) {
                        if (val==values[ii])
                        {
                            arr[i].checked=true;
                        }
                    }
                }
            }
            return arr;
        }

        this.gettreedata=function (arr)
        {
            var ids='';
            for (var i = 0; i < arr.length; i++) {

                ids+=arr[i]['value']+',';
                var children=arr[i].children || [];
                ids+=layui.rnform.gettreedata(children);
            }
             return ids;
        }

        this.removeAll = function (ids = '') {

            document.querySelectorAll('#' + ids + ' td[lay-ymd].layui-this').forEach(function (dom) {
                dom.classList.remove('layui-this');
            });
        }

        this.dateSelect = function (values, ids) {
            layui.rnform.removeAll(ids);
            values.forEach(function (val) {
                var dom = document.querySelector('#' + ids + ' td[lay-ymd="' + val.replace(/-0([1-9])/g, '-$1') + '"]');
                dom && dom.classList.add('layui-this');
            });
        }

        this.UploadAction = function (data) {

            var elem = "#upload_" + data.elem_id;
            var type = data.cat || '';
            var size = data.size || '';
            var file_num = data.num || 1;
            var multiple = data.file;
            var tag = data.tag;
            var accept="images";
            if (tag=='upfile')
            {
                accept="file";
            }
            layui.upload.render({
                elem: elem,
                url: '/admin/file/upload/',
                accept: accept,
                size: size,
                multiple: multiple,
                number: file_num,
                data: {
                    tag: tag,
                    type: type,
                },
                before: function (res) {
                    layer.load();
                },
                done: function (res) {
                    layui.layer.closeAll('loading');
                    if (res.code == 200) {
                        var src = res.data.location || '';
                        var that = $(elem).parent('.upfile').parent('.layui-input-block');
                        var imgHtml = '<div class="upload_list">'
                            + '<a href="javascript:void(0);" onclick="layui.public.delUploadFile(this)" class="img-file-del layui-icon layui-icon-close-fill"></a>'
                            + '<img layer-src="' + src + '" src="' + src + '" alt="">'
                            + '</div>';
                        if (file_num == 1) {
                            $(that).find(".upload_list").remove();
                            $(that).find('.upload_con').append(imgHtml);
                            $(that).find(".upload_list").css('width', 'auto')
                            $(that).find('.layui-input').val(src);
                        } else {
                            $(that).find('.upload_con').append(imgHtml);
                            var input_val = $(that).find('.layui-input').val();
                            if (input_val) {
                                input_val += ",";
                            }
                            $(that).find('.layui-input').val(input_val + src);
                        }
                        layui.public.layerphotos($(that).find('.upload_list'));
                    } else {
                        layui.layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function (res) {
                    layui.layer.msg('请求出错，请稍后再试', {icon: 2});
                }
            })
        }

        this.SelectAction = function (res = []) {
            var cat = res.tag || 'select';
            var field = res.field;
            var elem_id = res.elem_id;
            var is_all = res.is_all || 2;
            var strict = res.strict || false;

            var radio = true;
            var clickClose = true;
            if (is_all == 1) {
                radio = false;
                clickClose = false;
            }
            if (cat == 'select' && radio == true) {
                return;
            }
            var rows = res.data || [];
            var data = rows.rows || [];
            var initValue = '' + res.value + '' || '';
            if (initValue) {
                initValue = initValue.split(',');
            } else {
                initValue = [];
            }
            var tree_show = false;
            if (cat == 'tree') {
                tree_show = true;
            }
            var cascader_show = false;
            if (cat == 'cascader') {
                cascader_show = true;
            }
            xmSelect.render({
                prop: {
                    name: 'label',
                    value: 'value',
                },
                tree: {
                    show: tree_show,
                    expandedKeys: true,

                    strict: strict,
                },
                cascader: {
                    show: cascader_show,
                    strict: true,
                    indent: 200,
                },
                initValue: initValue,
                el: '#focus_' + elem_id,
                radio: radio,
                clickClose: clickClose,
                filterable: true,
                name: field,
                theme: {
                    color: theme_color,
                },
                data: data,
                on: function (res) {
                    try {
                        SelectAllAction(res, xm_field);
                    } catch (err) {
                        console.log(err)
                    }
                },
                done: function (res) {
                    $("#focus_" + elem_id + " xm-select").click(function () {
                        xm_field = field;
                    })
                }
            })
        }
        this.JsonTable = function (data = []) {
            var layout = layui.rnform.layout(data);
            var tableid = "table_" + data.elem_id;
            var value = data['value'] || [];
            var content = '<textarea name="' + data.field + '" id="focus_' + data.elem_id + '" class="layui-input" style="display: none">' + JSON.stringify(value) + '</textarea>';
            content += '<div class="' + tableid + ' f_table"><table id="' + tableid + '" lay-filter="filter_' + data.elem_id + '"></table></div>';
            var str = layout.replace('<content>', content);
            return str;
        }

        this.grid = function (data = []) {
            var tabcols = data['children'] || [];
            var elem_id = data['elem_id'] || '';
            var col=data.col || 12;
            var str = ' <div class="design-box layui-col-xs12 layui-col-md' + col+ '">';

            for (var i = 0; i < tabcols.length; i++) {
                tabcols[i].num = tabcols[i].num || 6;
                tabcols[i].children = tabcols[i].children || [];
                str += '<div class="design-rows layui-col-xs12 layui-col-md' + tabcols[i].num + '">';
                str += '<div class="j-fixed-grid-design ays-ctrl-container layui-row layui-col-space10" id="' + elem_id + '_s_' + i + '">';
                str += layui.rnform.GetForm(tabcols[i].children);
                str += '</div>';
                str += '</div>';
            }
            str += '</div>';
            return str;
        }

        this.card = function (data = []) {
            var elem_id = data['elem_id'] || '';
            var tabcols = data['children'] || [];
            var str = '<div class="layui-col-xs12 layui-col-md' + data.col + '">';
            str += '<div class="layui-card form_card">';
            str += '        <div class="layui-card-header">' + data['title'] + '</div>';
            str += '        <div class="layui-card-body j-fixed-grid-design ays-ctrl-container" id="' + elem_id + '_s_0">';
            str += layui.rnform.GetForm(tabcols[0].children);
            str += '        </div>';
            str += '      </div>';
            str += '      </div>';
            return str;
        }


        this.tab = function (data = []) {

            var tabcols = data['children'] || [];
            var elem_id = data['elem_id'] || '';
            var col=data.col || 12;
            var str = '<div class="layui-col-xs12 layui-col-md' + col + '">';
            str += '<div class="layui-tab layui-tab-brief">';
            str += '  <ul class="layui-tab-title">';
            for (var i = 0; i < tabcols.length; i++) {
                tabcols[i].name = tabcols[i].name || '';
                tabcols[i].children = tabcols[i].children || [];
                var css = '';
                if (i == 0) {
                    css = 'layui-this';
                }
                str += '<li class="' + css + '">' + tabcols[i].name + '</li>';
            }
            str += '  </ul>';
            str += '  <div class="layui-tab-content">';
            for (var i = 0; i < tabcols.length; i++) {
                var css = '';
                if (i == 0) {
                    css = 'layui-show';
                }
                str += '<div class="layui-tab-item ' + css + ' j-fixed-grid-design ays-ctrl-container" id="' + elem_id + '_s_' + i + '">';
                str += layui.rnform.GetForm(tabcols[i].children);
                str += '</div>';
            }
            str += '  </div>';
            str += '</div>';
            str += '      </div>';
            return str;
        }


        this.flex = function (data = []) {
            var elem_id = data['elem_id'] || '';
            var flex = data['children'] || [];
            var str = '<div class="layui-col-xs12 layui-col-md' + data.col + '">';
            str += '<div class="j-fixed-grid-design ays-ctrl-container ruinuo_flex" id="' + elem_id + '_s_0">';
            str += layui.rnform.GetForm(flex[0].children);
            str += '      </div>';
            str += '      </div>';
            return str;
        }


        this.LineText = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var range_field = data.range_field || '';
            var range_value = data.range_value || '';
            var range = data.range || '';
            var show_value = data.value || '';
            var content = '';
            var t_html = '';
            if (data.tag == 'datetime' && range_field) {
                var field = data.field || '';
                t_html += '<input type="hidden" name="' + field + '"  value="' + data.value + '">';
                t_html += '<input type="hidden" name="' + range_field + '"  value="' + range_value + '">';
                if (data.value && range_value) {
                    var show_value = data.value + ' ' + range + ' ' + range_value;
                }
            }
            content += '<input ' + attribute + ' autocomplete="off"  value="' + show_value + '" class="layui-input">' + t_html;
            var str = layout.replace('<content>', content);
            return str;
        }


        this.RowsText = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var style = 'style="min-height: ' + data.height + 'px;"';
            var content = '<textarea ' + attribute + ' autocomplete="off" class="layui-textarea" ' + style + '>' + data.value + '</textarea>';
            var str = layout.replace('<content>', content);
            return str;
        };

        this.switch = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var checkbox = "";
            if (data.value == 1) {
                checkbox = "checked";
            }
            var content = '<input type="checkbox" ' + checkbox + ' value="1" name="' + data.field + '" lay-filter="SwitchField" lay-skin="switch" lay-text="' + data.text + '"/>';
            var str = layout.replace('<content>', content);
            return str;
        }
        this.radio = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var content = '';
            var rows = data.data || [];
            rows = rows.rows || [];
            for (var i = 0; i < rows.length; i++) {
                var checked = '';
                if (data.value == rows[i].value) {
                    checked = " checked";
                }
                content += '<input type="radio" ' + attribute + ' value="' + rows[i].value + '" title="' + rows[i].label + '"' + checked + ' lay-filter="radio_filter">';
            }
            var str = layout.replace('<content>', content);
            return str;
        }

        this.checkbox = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var content = '';
            var rows = data.data || [];
            rows = rows.rows || [];
            var value = data.value || '';
            var arr = value.split(',');
            var v_str = '';
            for (var i = 0; i < arr.length; i++) {
                v_str += '[' + arr[i] + ']';
            }
            for (var i = 0; i < rows.length; i++) {
                var checked = '';
                if (v_str.indexOf("[" + rows[i].value + "]") !== -1) {
                    checked = " checked";
                }
                content += '<input type="checkbox" ' + attribute + ' value="' + rows[i].value + '" title="' + rows[i].label + '"' + checked + ' lay-filter="checkbox_filter">';
            }
            var str = layout.replace('<content>', content);
            return str;
        }

        this.select = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var content = '<div ' + attribute + '></div>';
            var is_all = data.is_all || 2;
            var tag = data.tag;
            var field = data.field || data.elem_id;
            if (is_all != 1 && tag == 'select') {
                var placeholder = data.placeholder || '请选择';
                var rows = data.data || [];
                var item = rows.rows || []
                var no_value=data.no_value || 2;
                var content = '<select name="' + field + '" lay-filter="select_filter">';
                if (no_value==2)
                {
                    content += '      <option value="">' + placeholder + '</option>';
                }

                for (var i = 0; i < item.length; i++) {
                    var children = item[i].children || [];
                    if (children.length == 0) {
                        var selected = '';
                        if (item[i].value == data.value) {
                            selected = " selected";
                        }
                        content += '      <option value="' + item[i].value + '"' + selected + '>' + item[i].label + '</option>';
                    } else {
                        content += '<optgroup label="' + item[i].label + '">';
                        for (var ii = 0; ii < children.length; ii++) {
                            var selected = '';
                            if (children[ii].value == data.value) {
                                selected = " selected";
                            }
                            content += '      <option value="' + children[ii].value + '"' + selected + '>' + children[ii].label + '</option>';
                        }
                        content += '</optgroup>';
                    }

                }

                content += '    </select>';
            }
            var str = layout.replace('<content>', content);
            return str;
        }
        this.color = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var content = '<input ' + attribute + ' autocomplete="off"  value="' + data.value + '" class="layui-input">';
            content += '<div class="focus_' + data.elem_id + ' colorpicker" data-id="focus_' + data.elem_id + '"></div>'
            var str = layout.replace('<content>', content);
            return str;
        }

        this.rate = function (data = []) {
            var layout = layui.rnform.layout(data);
            var attribute = layui.rnform.attribute(data);
            var content = '<input ' + attribute + ' autocomplete="off"  value="' + data.value + '" class="layui-input">';
            content += '<div class="focus_' + data.elem_id + ' ratepicker" data-id="focus_' + data.elem_id + '"></div>'
            var str = layout.replace('<content>', content);
            return str;
        }
        this.slider = function (data = []) {
            var layout = layui.rnform.layout(data);
            var content = '<input type="hidden" id="focus_' + data.elem_id + '" name="' + data.field + '"  value="' + data.value + '" class="layui-input">';
            content += '<div id="slider_' + data.elem_id + '" data-id="focus_' + data.elem_id + '"></div>';
            var str = layout.replace('<content>', content);
            return str;
        }


        this.TipsLegend = function (data = []) {
            var colclass = "";
            var str = "";
            var col = data.col || 12;
            switch (data.tag) {
                case "tips":
                    str = '<blockquote class="layui-elem-quote tips_legend layui-col-xs' + col + ' ' + colclass + '">' + data.content + '</blockquote>';
                    break;
                case "legend":
                    str = '<fieldset class="layui-elem-field layui-field-title layui-col-xs' + col + ' ' + colclass + '"><legend class="tips_legend">' + data.content + '</legend></fieldset>';
                    break;
            }
            return str;
        }

        this.OpenChoose = function (elem_id = '') {
            var data = datedata[elem_id];
            var data_cat = data.data_cat || '1';
            var title = '添加';
            if (data_cat == 'choose') {
                title = '选择';
            }
            var width=data.width || '90%';
            var height=data.height || '90%';
            var area=[width,height];
            var url = data.url || '';
            var is_all = data.is_all || 2;
            var value = data.value || '[]';
            var tag=data.tag || '';
            var cols=data.cols || [];
            parent.layer.open({
                type: 2,
                offset: 'auto',
                title: title,
                area: area,
                scrollbar: true,
                content: url,
                btnAlign: 'r',
                btn: ['确定', '取消'],
                shade: 0.3,
                yes: function (index) {
                    var iframe = parent.window['layui-layer-iframe' + index];
                    var backdata = iframe.layui.rntable.getChooseData() || false;
                    if (backdata==false)
                    {
                    return false;
                    }
                    switch (tag)
                    {
                        case "jsontable":
                            var tableelem='table_'+elem_id;
                            var tableBak = layui.table.cache[tableelem] || [];
                            var key_id='';
                            var key_ids='';
                            for (var ii = 0; ii < cols.length; ii++) {
                                var is_key=cols[ii]['is_key'] || 2;
                                if (is_key==1)
                                {
                                    key_id=cols[ii]['field'];
                                    var d_field=cols[ii]['value'] || '';
                                    var fields=d_field.split('d.');
                                    key_ids=fields[1] || '';
                                }
                            }
                            for (var i = 0; i < backdata.length; i++) {
                                var is_add=1;
                                if (key_id && key_ids)
                                {
                                    for (var ii = 0; ii < tableBak.length; ii++) {
                                        if (tableBak[ii][key_id]==backdata[i][key_ids])
                                        {
                                            is_add=2;
                                        }
                                    }
                                }

                                if (is_add==1)
                                {

                                    var newData={};
                                    for (var ii = 0; ii < cols.length; ii++) {
                                        var field=cols[ii]['field'] || '';
                                        if (field)
                                        {
                                            var d_field=cols[ii]['value'] || '';
                                            var fields=d_field.split('d.');
                                            if (fields.length==2)
                                            {
                                                newData[field]=backdata[i][fields[1]];
                                            }
                                            else
                                            {
                                                newData[field]=d_field;
                                            }
                                        }

                                    }
                                    tableBak.push(newData);
                                }
                            }
                            layui.table.reload(tableelem, {
                                data: tableBak, url: '',
                            });
                            layui.rntable.BackAction(tableBak, tableelem);
                            break;
                        default:
                    }
                    var bindex = parent.layer.getFrameIndex('layui-layer-iframe' + index);
                    parent.layer.close(bindex);

                }, success: function (layero, index) {
                    var iframe = parent.window['layui-layer-iframe' + index];
                }
            })
        }


        this.layout = function (data = []) {
            var required = data.required || '';
            var hide = data.hide || '';
            var help = data.help || '';
            var suffix = data.suffix || '';
            var prefix = data.prefix || '';
            var group_suffix = data.group_suffix || '';
            var group_prefix = data.group_prefix || '';
            var col = data.col || 12;
            var title = data.title || '';
            var tag = data.tag || '';
            var field = data.field || '';
            var hide_label = data.hide_label || '';
            var value = data.value || '';
            switch (tag) {
                case "datetime":
                    prefix = 'icon-riqi';
                    break;
                case "email":
                    prefix = 'icon-youxiang';
                    break;
                case "phone":
                    prefix = 'icon-shouji1';
                    break;
                case "password":
                    prefix = 'icon-mima';
                    break;
                case "openselect":
                    suffix = 'icon-arrow-right';
                    break;
                case "dateall":
                    col += " ruinuo_dateall";
                    break;
            }
            if (hide == 1) {
                col += " ruinuo_yes_none";
            }
            if (field) {
                col += " " + field + "_box";
            }
            var required_class = "";
            if (required == 1) {
                required_class = " layui-form-item-required";
            }
            var suffix_class = "";
            if (group_suffix || group_prefix) {
                suffix_class += " layui-input-group";
            }

            if ((suffix || prefix)) {
                suffix_class += " layui-input-wrap";

            }
            var str = ' <div class="design-box layui-col-xs12 layui-col-md' + col + '">';
            if (hide_label != 1) {
                str += '<label class="layui-form-label' + required_class + '">' + title + '</label>';
            } else {
                suffix_class += ' block_margin_left_0'
            }
            str += '                    <div class="layui-input-block ' + suffix_class + '">';
            if (group_prefix) {
                str += '                   <div class="group_input group_prefix">' + layui.rnform.GetFix(group_prefix) + '</div>';
            }
            if (prefix) {
                str += '                   <div class="layui-input-prefix">' + layui.rnform.GetFix(prefix) + '</div>';
            }
            str += '<content>';
            if (group_suffix) {
                str += '                   <div class="group_input group_suffix">' + layui.rnform.GetFix(group_suffix) + '</div>';
            }
            if (suffix) {
                str += '                   <div class="layui-input-suffix layui-input-split">' + layui.rnform.GetFix(suffix) + '</div>';
            }
            if (tag == 'upimg' || tag == 'upfile') {
                var c = '文件';
                if (tag == 'upimg') {
                    var c = '图片';
                }
                var is_file=data.is_file || 1;
                if (is_file==1)
                {
                str += '<div class="upfile display_flex">' +
                    '<button class="pear-btn pear-btn-sm" type="button" id="upload_' + data.elem_id + '"><i class="layui-icon layui-icon-addition"></i>上传' + c + '</button>' +
                    '<button class="pear-btn pear-btn-sm" style="display: none" type="button" onclick="layui.rnform.ChooseFile(this,\'' + tag + '\')"><i class="layui-icon layui-icon-spread-left"></i>选择' + c + '</button>';
                if (help) {
                    str += ' <div class="layui-form-help" style="padding-top: 0px;margin-left: 5px"><i class="layui-icon layui-icon-tips"></i>' + help + '</div>';
                }
                str += '</div>';
                }
                var imgHtml = '';

                var item = value.split(',');

                for (var i = 0; i < item.length; i++) {
                    if (item[i])
                    {
                    imgHtml += '<div class="upload_list">';
                        if (is_file==1) {
                            imgHtml += '<a href="javascript:void(0);" onclick="layui.public.delUploadFile(this)" class="img-file-del layui-icon layui-icon-close-fill"></a>';
                        }
                        imgHtml+=  '<img layer-src="' + item[i] + '" src="' + item[i] + '" style="cursor: pointer" onclick="layui.public.layerphotos(this,3)">';
                        imgHtml+=  '</div>';
                    }
                }

                str += '<div class="upload_con">' + imgHtml + '</div>';
            }
            if (tag == 'showtree') {
                str += ' <div id="showtree_' + data.elem_id + '"></div>';
            }
            if (help && tag != 'upimg' && tag != 'upfile') {
                str += ' <div class="layui-form-help"><i class="layui-icon layui-icon-tips"></i>' + help + '</div>';
            }
            str += '                   </div>';
            str += '                </div>';
            return str;
        };
        this.ChooseFile = function (obj, tag) {

        };

        this.GetFix = function (str = '') {
            var icon = str.split('icon');
            if (icon.length == 1) {
                return str;
            } else {
                return '<i class="ruinuo-icon ' + str + '"></i>'
            }
        };

        this.attribute = function (data = []) {
            var readonly = data.readonly || '';
            var disabled = data.disabled || '';
            var follow = data.follow || '';
            var field = data.field || '';
            var tag = data.tag || '';
            var type = data.type || 'text';
            var placeholder = data.placeholder || '';
            switch (tag) {
                case "password":
                    type = 'password';
                    break;
                case "number":
                    type = 'number';
                    break;
                case "textarea":
                case "htmledit":
                    type = 'textarea';
                    break;
                case "rate":
                case "showtree":
                    type = 'hidden';
                    break;
                case "icon":
                case "openselect":
                case "upimg":
                case "upfile":
                    readonly = 1;
                    type = 'hidden';
                    break;
                case "datetime":
                    readonly = 1;
                    var range_field = data.range_field || '';
                    if (range_field) {
                        var field = '';
                    }
                    break;
                case "tags":
                    placeholder = '回车确定';
                    break;
            }

            var str = "";
            if (field) {
                str += ' name="' + field + '"';
            }
            str += ' id="focus_' + data.elem_id + '"';
            if (type) {
                str += ' type="' + type + '"';
            }

            if (readonly == 1) {
                str += ' readonly';
            }
            if (disabled == 1) {
                str += ' disabled';
            }

            if (placeholder) {
                str += ' placeholder="' + placeholder + '"';
            }

            if (follow == 1) {
                str += ' onclick="layui.rnform.GetFollow(this)"';
            }

            if (data.tag == 'number') {
                str += ' lay-affix="number"';
            }
            if (data.tag == 'password') {
                str += ' lay-affix="eye"';
            }
            return str;
        }


        this.ChangeOnclick = function (field = '') {

            if (field == '') {
                $(".ruinuo_flex").each(function () {
                    $(this).find('.design-box').eq(0).removeClass('layui-col-xs12 layui-col-md12')
                })
            }

            /*点击事件*/
            var arr = onclickarr || [];
            for (var k = 0; k < arr.length; k++) {
                if (field) {
                    if (arr[k]['field'] == field) {
                        layui.rnform.EndeOnclick(arr[k]);
                    }
                } else {
                    layui.rnform.EndeOnclick(arr[k]);
                }
            }
        }

        this.EndeOnclick = function (data = []) {
            var value = '';
            var field = data.field || '';
            switch (data['tag']) {
                case "radio":
                    value = $("input[name='" + field + "']:checked").val() || '';
                    break;
                case "checkbox":
                    $("input[name='" + field + "']:checked").each(function () {
                        value += $(this).attr('value') + ',';
                    })
                    break;
                case "select":
                    value = $("select[name='" + field + "']").val() || '';
                    break;
            }
            var v_item = value.split(',');
            var v2_item = data.value.split(',');
            var c = 2;
            var f_item = data.changefield || [];
            for (var k = 0; k < v_item.length; k++) {
                for (var kk = 0; kk < v2_item.length; kk++) {
                    if (v_item[k] == v2_item[kk]) {
                        c = 1;
                    }
                }
            }
            for (var k = 0; k < f_item.length; k++) {
                var required = f_item[k]['required'] || 2;
                if (required == 1) {
                    $('.' + f_item[k]['field'] + '_box .layui-form-label').addClass('layui-form-item-required')
                }
                if (c == 1) {
                    $('.' + f_item[k]['field'] + '_box').show();
                } else {
                    $('.' + f_item[k]['field'] + '_box').hide();
                }
            }
        }



    }
    exports(MOD_NAME, context);
});
